/**
 * Project Name:fastPlat
 * File Name:PositionController.java
 * Package Name:com.fast.cms.web.controller.admin
 * Date:2014年12月17日下午3:42:15
 * Copyright (c) 2014, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.user.web.controller.dept;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.fast.base.db.page.PageView;
import com.fast.base.db.page.QueryResult;
import com.fast.base.util.StringUtils;
import com.fast.base.util.data.MathUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.WebUtil;
import com.fast.base.web.BaseController;
import com.fast.common.userCache.AdminCacheUtils;
import com.fast.common.userCache.UserRequestUtils;
import com.fast.user.model.admin.AdminUser;
import com.fast.user.model.dept.Position;
import com.fast.user.service.dept.PositionService;

/**
 * ClassName:PositionController <br/>
 * 用途: 职位信息表 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @version  
 * @since    JDK 1.6
 * @see
 * Date:     2014年12月17日 下午3:42:15 <br/> 	 
 */
@Controller
@RequestMapping("/position")
public class PositionController extends BaseController{
	

	
	@Resource(name="positionService")
	private PositionService beanService;

	/**
	 * 编辑[职位]信息
	 * @param formData
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/editPosition.do")
	public ResponseData editPosition() {
		ResponseData responseData = new ResponseData();
		Position position = null;
		try {
			int id = webUtil.getInt("id");
			if (id > 0) {
				position = this.beanService.queryForBeanById(Position.class, id);
				position = (Position) webUtil.bindDataToBean(position);
			} else {
				position = new Position();
				position = (Position) webUtil.bindDataToBean(position);
				position.setUuid(MathUtils.getTheOnlyStr());
				UserRequestUtils.bindCreaterInfo(position);
			}
			// 当存在UUID时的处理
			UserRequestUtils.bindEditorInfo(position);
			if (this.beanService.merge(position) != null) {
				responseData.setMsg("编辑编号为[" + position.getUuid() + "]操作成功");
			}
			responseData.setCode("00");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMsg("编辑编号为[" + position.getUuid() + "]操作失败");
		}
		return responseData;
	}
	
	/**
	 * 批量编辑[职位]信息
	 * @param formData
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/editAllPosition.do")
	public ResponseData editAllPosition(String formData) {
		List<Position> deptList = JSONArray.parseObject(formData.replace("&quot;", "\'"), ArrayList.class);
		AdminUser curAdminUser = AdminCacheUtils.getLoginAdminUser();
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = deptList.size();
			int success     = 0;
			int failure     = 0;
			for (Position position : deptList) {
				position.setPositionName(position.getPositionName());
				//设计唯一UUID
				position.setUuid(MathUtils.getTheOnlyStr());
				//设置操作人编辑人
				position.setCreater(curAdminUser.getUserName());
				position.setCreaterId(curAdminUser.getId());
				position.setCreateTime(new Date());
				position.setEditorId(curAdminUser.getId());
				position.setEditorName(curAdminUser.getUserName());
				position.setEditTime(new Date());
				boolean actionFlag = beanService.saveOrModify(position, " where uuid='"+position.getUuid()+"'");
				if(actionFlag){
					success++;
				}else{
					failure++;
				}
				Thread.sleep(50);
			}
		    responseData.setMsg("批量操作结果为：共添加数据["+actionCount+"],成功保存["+success+"],失败保存["+failure+"]");
			responseData.setCode("00");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMsg("批量保存数据失败！");
		}
		return responseData;
	}
	
	/**
	 * 删除[职位]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delPosition.do")
	public ResponseData delPosition(int id) {
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = beanService.deleteById(Position.class, id);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.setMsg("删除部门编号为["+id+"]操作成功");
			}else{
				responseData.setMsg("删除部门编号为["+id+"]操作失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("删除部门编号为["+id+"]操作失败");
		}
		return responseData;
	}
	
	/**
	 * 删除多个[[职位]]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delMorePosition.do")
	public ResponseData delMorePosition(String ids) {
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = beanService.deleteByIds(Position.class, ids);
			responseData.setCode("00");
			responseData.setMsg("删除[职位]编号为["+ids+"]操作成功,共成功["+actionCount+"]");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("删除[职位]["+ids+"]操作失败");
		}
		return responseData;
	}
	
	/**
	 * 得到[职位]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getJsonDetail.do")
	public ResponseData getPositionDetail(int id) {
		ResponseData responseData = new ResponseData();
		Position bean = beanService.queryForBeanById(Position.class, id);
		try {
			responseData.setCode("00");
			responseData.setData(bean);
			responseData.setMsg("获取信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("获取信息失败");
		}
		return responseData;
	}
	
	/**
	 * 删除[职位]信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/getPositionDetail.do")
	public String getPositionDetail(int id,HttpServletRequest request,HttpServletResponse response) {
		WebUtil wu = new WebUtil(request,response);
		Position bean = beanService.queryForBeanById(Position.class, id);
		try {
			wu.setAttr("Position", bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/admin/admin/PositionDetail";
	}
	
	/**
	 * 得到[职位]列表
	 * @param dtGridPager Pager对象
	 * @param request 请求对象
	 * @param response 响应对象
	 * @throws IOException 
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/getPositionList.do", method=RequestMethod.POST)
	public Map<String,Object> getPositionList(HttpServletResponse response) throws IOException{
		Map<String,Object> gridMap = new HashMap<String,Object>();
		int page = webUtil.getInt("page");//当前页
  		int pagesize = webUtil.getInt("rows");//每页面显示多少条
  		String companyUUID = webUtil.getString("companyUUID");
  		String deptUUID = webUtil.getString("departmentUUID");
  		String deptName = webUtil.getString("deptName");
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		if(StringUtils.isNotBlank(companyUUID)){
			sb.append(" and companyUUID='"+companyUUID+"'");
		}
		
		if(StringUtils.isNotBlank(deptUUID)){
			sb.append(" and deptUUID='"+deptUUID+"'");
		}
		
		if(StringUtils.isNotBlank(deptName)){
			sb.append(" and deptName='"+deptName+"'");
		}
		
		String ordersql = " order by edit_time desc ";
		QueryResult<Position>  queryResult = this.beanService.queryForBeanListByPage(Position.class, sb.toString(), ordersql,(page-1)*pagesize,pagesize);
		PageView<Position> pageView = new PageView<Position>();
	    pageView.setCurrentPage(page);
		pageView.setQueryResult(queryResult);
		gridMap.put("total", pageView.getTotalRecord());
		gridMap.put("rows", pageView.getRecords());
 		return gridMap;
	}
	
	/**
	 * 得到部门职位相关信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loadPositionByDeptId.do")
	public List<Position> loadPositionByDeptId(String deptUUID) {
		List<Position> deptList = this.beanService.queryForBeanList(Position.class, "where deptUUID='"+deptUUID+"'");
		return deptList;
	}
}

